import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from delete import delete_data
from aws_model.athena_submit import submit
from project_utils.get_washer_mappings import match_mappings, not_match_mappings, merge_mapping
from config import *
from concat_sql.washer_column_mapping import insert_washer_mapping
import datetime
from base_class.base_utils import spend_time
import json
from base_class.inspect_logging import print_to_log
from aws_model.aws_stepfunc import send_task_success, send_task_failure
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED

tablename = hbase_map_table
mapping1 = match_mappings(tablename)
mapping2 = not_match_mappings(tablename)
mapping3 = {**mapping1, **mapping2}


@spend_time("washer_daily2ods 耗时：")
def washer_daily2ods(event):
    table_name = sys._getframe().f_code.co_name
    if 'is_hour' in event and event['is_hour'] == 'true':
        source_table = washer_hbase_table_hour
        target_table = washer_target_table_hour
        is_hour = True
    else:
        source_table = washer_hbase_table
        target_table = washer_target_table
        is_hour = False
    date = event.get('p_date')
    if not date:
        today = datetime.datetime.today()
        # 昨天
        date = (today - datetime.timedelta(days=1)).strftime('%Y-%m-%d')

    # 读取orginal现有materia，和数据库读取的material对比
    query_rs = submit(f"select distinct material from {source_table} where p_date = '{date}'")
    if query_rs['statusCode'] == 500:
        send_task_failure(str(e), f'执行{sys._getframe().f_code.co_filename}错误', tasktoken)
        raise Exception(f"查询sql错误：{query_rs}")

    # 如果数据库中不存在这个material，则添加进映射dict
    exists_materials = {}
    if 'result' in query_rs:
        one = list(mapping2.values())[0]
        for row in query_rs['result']:
            exists_materials[row[0]] = ''
            if row[0] not in mapping2 and row[0] not in mapping1:
                mapping3[row[0]] = one
    # 删除数据库映射中多余的material
    li = []
    for key in mapping3:
        if key not in exists_materials:
            li.append(key)
    for key in li:
        del mapping3[key]

    (mappings, material_list) = merge_mapping(mapping3)
    for m in material_list:
        delete_data(target_table, date, m)

    with ThreadPoolExecutor(4) as pool:
        results = []
        for mapping_json in mappings:
            for category, materials in mappings[mapping_json].items():
                results.extend(pool.submit(insert_washer_mapping,
                                           is_hour,
                                           mapping_json,
                                           m,
                                           date) for m in materials)
        wait(results, return_when=ALL_COMPLETED)

    output = {"msg": f"{table_name} submit ok", "Payload": {}}
    output = json.dumps(output)
    step_response = send_task_success(tasktoken, output)
    print_to_log("step_response: ", step_response)


if __name__ == '__main__':
    try:
        event = json.loads(sys.argv[1])
        tasktoken = event.get('TaskToken')
        washer_daily2ods(event)
    except Exception as e:
        print_to_log("任务 失败")
        print_to_log("原因： ", str(e))
        send_task_failure(str(e), f'执行{sys._getframe().f_code.co_filename}错误', tasktoken)
